package com.danger.Model.action;

import android.content.Context;
import android.text.TextUtils;

import com.danger.Model.bean.ObjectInfo;
import com.danger.Model.listener.OnUpdateListener;
import com.danger.Model.listener.OnUploadPhotoListener;
import com.danger.Util.CheckCode;

import java.io.File;

import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;

/**
 * Created by danger on 2016/3/31.
 */
public class ModifyModelImpl implements ModifyModel {

    private Context context;
    private String NAME_IS_NULL = "名称为空";
    private String DESCRIBE_IS_NULL = "描述为空";
    private String IS_RIGHT = "正确";

    public ModifyModelImpl(Context context) {
        this.context = context;
    }

    @Override
    public void modify(ObjectInfo objectInfo, final OnUpdateListener listener) {
        objectInfo.update(context, objectInfo.getObjectId(), new UpdateListener() {
            @Override
            public void onSuccess() {
                listener.onSuccess();
            }

            @Override
            public void onFailure(int i, String s) {
                listener.onError(s);
            }
        });
    }

    @Override
    public void uploadPhoto(String path, final OnUploadPhotoListener listener) {
        final BmobFile bmobFile = new BmobFile(new File(path));
        bmobFile.uploadblock(context, new UploadFileListener() {
            @Override
            public void onSuccess() {
                listener.onSuccess(bmobFile.getFileUrl(context));
            }

            @Override
            public void onFailure(int i, String s) {
                listener.onError(CheckCode.checkByCode(i));
            }
        });
    }

    @Override
    public String check(ObjectInfo objectInfo) {
        if (TextUtils.isEmpty(objectInfo.getTitle().toString().trim())) {
            return NAME_IS_NULL;
        }
        if (TextUtils.isEmpty(objectInfo.getDescribe().toString().trim())) {
            return DESCRIBE_IS_NULL;
        }
        return IS_RIGHT;
    }
}
